#region Using

using System;
using System.Xml;
using System.IO;
using System.Globalization;
using System.Collections.Generic;
using System.Collections.Specialized;
using BlogEngine.Core;
using BlogEngine.Core.Provider.Interfaces;

#endregion

namespace BlogEngine.Core.Provider.XML
{
	/// <summary>
	/// A storage provider for BlogEngine that uses XML files.
	/// <remarks>
	/// To build another provider, you can just copy and modify
	/// this one. Then add it to the web.config's BlogEngine section.
	/// </remarks>
	/// </summary>
	public class CIPingServices : IPingServices
	{

		private static string _fileName = Provider.Folder + "pingservices.xml";

		/// <summary>
		/// Loads the ping services.
		/// </summary>
		/// <returns></returns>
		public StringCollection Load()
		{
			if (!File.Exists(_fileName))
				return new StringCollection();

			StringCollection col = new StringCollection();
			XmlDocument doc = new XmlDocument();
			doc.Load(_fileName);

			foreach (XmlNode node in doc.SelectNodes("services/service"))
			{
				if (!col.Contains(node.InnerText))
					col.Add(node.InnerText);
			}

			return col;
		}

		/// <summary>
		/// Saves the ping services.
		/// </summary>
		/// <param name="services">The services.</param>
		public void Save(StringCollection services)
		{
			if (services == null)
				throw new ArgumentNullException("services");

			using (XmlTextWriter writer = new XmlTextWriter(_fileName, System.Text.Encoding.UTF8))
			{
				writer.Formatting = Formatting.Indented;
				writer.Indentation = 4;
				writer.WriteStartDocument(true);
				writer.WriteStartElement("services");

				foreach (string service in services)
					writer.WriteElementString("service", service);

				writer.WriteEndElement();
			}
		}

	}
}
